package com.longrise.android.byjk.plugins.login;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.main.MainActivity;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.plugins.publicWeb.PublicWebJumpUtil;
import com.longrise.common.utils.ActivityUtil;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.datasource.local.sp.CacheUtils;
import com.longrise.android.byjk.utils.PhoneUtils;
import com.longrise.android.byjk.widget.view.InputBoxView;

/**
 * Created by callxkj on 2017/3/23.
 * 20170323改版的简单注册
 */

public class EasyRegistActivity extends BaseActivity implements View.OnClickListener, TextWatcher, CompoundButton.OnCheckedChangeListener {


    private Toolbar mToolbar;
    private TextView mTvtitle;
    private TextView mTvgetVf;
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            mTvgetVf.setText("重新获取(" + millisUntilFinished / 1000 + "s)");

        }

        @Override
        public void onFinish() {
            getVftoNorm();
        }
    };
    private EditText mEtphn;
    private Button mBtregist;
    private InputBoxView mIbvVfcode;
    private CheckBox mCheckBox;
    private TextView mTvprotocol;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_easyregist;
    }

    @Override
    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        mTvtitle = (TextView) findViewById(R.id.bb_toolbar_middle_title);
        mTvtitle.setText(AppUtil.getString(R.string.easyregist_title));
        mTvtitle.setVisibility(View.VISIBLE);

        mCheckBox = (CheckBox) findViewById(R.id.easyregist_checkbox);
        mCheckBox.setChecked(true);

        mTvgetVf = (TextView) findViewById(R.id.forgetpasword_pic2);
        mEtphn = (EditText) findViewById(R.id.forgetpasword_text);
        mEtphn.setHint(AppUtil.getString(R.string.phone_num_hint));
        mIbvVfcode = (InputBoxView) findViewById(R.id.easyregist_vfcode);
        mTvgetVf.setClickable(true);

        mTvprotocol = (TextView) findViewById(R.id.easyregist_protocol_tv);

        mBtregist = (Button) findViewById(R.id.easyregist_confirm);
        mBtregist.setBackgroundResource(R.color.bt_cannot_click);
        initEvent();
        mBtregist.setClickable(false);
    }

    private void initEvent() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
            }
        });
        mTvgetVf.setOnClickListener(this);
        mBtregist.setOnClickListener(this);
        mEtphn.addTextChangedListener(this);
        mIbvVfcode.getEditText().addTextChangedListener(this);
        mTvprotocol.setOnClickListener(this);
        mCheckBox.setOnCheckedChangeListener(this);
    }

    /**
     * 让获取验证码的按钮恢复正常状态
     */
    private void getVftoNorm() {
        mTvgetVf.setClickable(true);
        mTvgetVf.setText("获取验证码");
        mTvgetVf.setTextColor(Color.parseColor("#FF9900"));
    }

    /**
     * 关闭当前页
     */
    private void close() {
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forgetpasword_pic2://获取短信验证码
                toGetVfCode();
                break;
            case R.id.easyregist_confirm: //注册按钮
                toRegist();
                break;
            case R.id.easyregist_protocol_tv://保宝用户注册协议
                PublicWebJumpUtil.jumpToPubWeb(this, "zcxy");
                break;
        }
    }

    /**
     * 获取短信验证码
     * checktype:验证类别("1":通过手机号注册;"2":通过手机号登录;"3":开通手机号快捷登录;)
     */
    private void toGetVfCode() {
        String mobile = mEtphn.getText().toString().trim(); //手机号码
        if (TextUtils.isEmpty(mobile)) {
            showToast(AppUtil.getString(R.string.phonenum_isnull));
            return;
        }
        if (!PhoneUtils.isPhoneNumber(mobile)) {
            showToast(AppUtil.getString(R.string.txsjh));
            return;
        }
        mTvgetVf.setClickable(false);
        mTvgetVf.setTextColor(Color.parseColor("#666666"));
        showLoadingDialog();
        GetMobileVfhelper.getInstance().getMobileVfCode(mContext, mobile, "1", new GetMobileVfhelper.MobileVfCodeCallback() {
            @Override
            public void onSuccess(String resultStatus) {
                if ("1".equals(resultStatus)) {
                    timer.start();
                } else {
                    getVftoNorm();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                dismissLoadingDialog();
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                getVftoNorm();
            }
        });
    }

    /**
     * 注册
     */
    private void toRegist() {
        String mobile = mEtphn.getText().toString().trim(); //手机号码
        String vfcode = mIbvVfcode.getText();
        showLoadingDialog();
        UserController.getInstance().mobileRegist(mContext, mobile, vfcode, new UserCallback() {
            @Override
            public void onSuccess(int status, String des) {
                //用手机号注册后 将登陆页的用户名置空.
                CacheUtils.setString(Control.USERNAME, "");
                showToast(des);
                if (status == UserCallback.HOME_PAGE) {
                    ActivityUtil.removeActivity(EasyRegistActivity.this);
                    ActivityUtil.finishAllAcivity();
                    startActivity(new Intent(mContext, MainActivity.class));
                    finish();
                }
            }

            @Override
            public void onFail(String des) {
                showToast(des);
            }

            @Override
            public void onFinish() {
                dismissLoadingDialog();
            }
        });
    }

    //-------------输入框字符变化----------------
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        setConfrimBtStatus();
    }
    //------------------------------------------

    //-------------checkbox选择状态变化----------------
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        setConfrimBtStatus();
    }
    //------------------------------------------

    /**
     * 检查是否全部填写
     *
     * @return 全部有填写 返回true
     */
    private boolean checkText() {
        //手机号
        String phoneNum = mEtphn.getText().toString().trim();
        if (TextUtils.isEmpty(phoneNum)) {
            return false;
        }
        //短信验证码
        String mIbvVfcodeText = mIbvVfcode.getText();
        if (TextUtils.isEmpty(mIbvVfcodeText)) {
            return false;
        }
        return true;
    }

    /**
     * 确定按钮是否为可点击
     */
    private void setConfrimBtStatus() {
        boolean b = checkText();
        boolean c = mCheckBox.isChecked();
        if (b && c) {
            mBtregist.setClickable(true);
            mBtregist.setBackgroundResource(R.drawable.selecter_yellowbtn);
        } else {
            mBtregist.setClickable(false);
            mBtregist.setBackgroundResource(R.color.bt_cannot_click);
        }
    }

}
